import axios from 'axios';
import qs from 'qs'
// import { Message } from 'element-ui';

axios.defaults.timeout = 5000;
// axios.defaults.baseURL = '';


//http request 拦截器
axios.interceptors.request.use(
    config => {
        config.headers = {
            // 'Content-Type': 'application/json'
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        // if(token){
        //   config.params = {'token':token}
        // }
        // console.log(config.data)
        config.data = qs.stringify(config.data)
        // config.data = JSON.parse(config.data)
        return config;
    },
    error => {
        return Promise.reject(err);
    }
);


//http response 拦截器
axios.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        return Promise.reject(error)
    }
)


// * 封装post请求

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        // console.log(data, 'post')
        axios.post(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}
/**
 * 封装get方法
 */

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}
